// package-store/pages/store-withdraw/index.js
import { ManageTargetInfoUpdateMixins } from '../../../behaviors/page';
import { WITHDRAW_TYPE, WITHDRAW_TYPE_TEXT } from '../../../constants/common';
import { setTitle, navBack, tipToast, tipSuccess } from '../../../common/env';
import { applyWithdraw } from '../../../api/sto';
import { getUserPrivacyInfo } from '../../../api/usr';

Page({
    mixins: [ManageTargetInfoUpdateMixins],
    data: {
        name: '', // 实名
        idCard: '', // 身份证号
        withdrawType: WITHDRAW_TYPE.change,
        availableAmount: 0,
    },
    async onLoad(options) {
        const { type, amount } = options;
        setTitle(WITHDRAW_TYPE_TEXT[type]);
        const info = await getUserPrivacyInfo();
        this.setData({
            withdrawType: type,
            availableAmount: amount,
            name: info.realName || '',
            idCard: info.idCardNo || '',
        });
    },
    handleChangeUserName(e) {
        this.setData({
            name: e.detail,
        });
    },
    handleChangeIdCard(e) {
        this.setData({
            idCard: e.detail,
        });
    },
    async handleWithdrawApply(e) {
        const amount = e.detail;
        const { idCard, name, withdrawType } = this.data;
        if (!name) {
            tipToast('收款人不能为空');
            return;
        }
        if (!idCard) {
            tipToast('身份证号不能为空');
            return;
        }
        const params = {
            target: withdrawType,
            amount,
            remark: WITHDRAW_TYPE_TEXT[withdrawType],
            realName: name,
            idCardNo: idCard,
        };
        const flag = await applyWithdraw(params);
        if (flag) {
            tipSuccess('提现成功');
            navBack();
        }
    },
})
